/**
 * Created by sun on 2016/8/4.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');



    app.useModule("ui.table");
    var toastr =require('toastr');

    var datepicker = require('datepicker');
    app.controller('typeCtrl', ['$scope','$http','$filter',function ($scope, $http,$filter) {

        $scope.select2Options = {
            minimumResultsForSearch: Infinity //不出现搜索框
        };
    	$scope.list={};
    	$scope.add={};
        $scope.orderParams = {
            url: "/admin/sysNoticeType/list"
        };
        datepicker($scope);

        //创建
        $scope.addOrderModal=function(){
            $scope.orderModal = !$scope.orderModal ;
        };

        //全局查询
        $scope.search = function() {
            $scope.ctrlScope.itemsearch.beginDateTime=$scope.ctrlScope.itemsearch.beginDateTimex;
            $scope.ctrlScope.itemsearch.endDateTime= $scope.ctrlScope.itemsearch.endDateTimex;
            $scope.ctrlScope.itemsearch.beginDateTime=$filter('date')($scope.ctrlScope.itemsearch.beginDateTime,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.endDateTime=$filter('date')($scope.ctrlScope.itemsearch.endDateTime,'yyyy-MM-dd');
            //表格查询参数设置
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            //表格刷新
            $scope.orderParams.reload();
        };

        //全局查询重置
        $scope.reset=function(){
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.ajaxParam={};
            $scope.orderParams.reload();
        };

        //创建提交
        $scope.adoptAdd = function(isValid) {
            if (!isValid) {
                toastr.error("信息填写有误");
            }
        	var parm=$scope.add;
            $http({
                method: 'POST',
                url: "/admin/sysNoticeType/add",
                data:parm,
            }).success(function(data) {

                if(data.result=="fail"){
                    toastr.error("创建失败");
                }else{
                    toastr.success("创建成功");
                    $scope.orderModal = !$scope.orderModal;
                    $scope.orderParams.reload();

                }
            }).error(function(data) {
            });
        };
        
        
        //编辑
        $scope.modifyModal=function(id){
        	$scope.detailsModal=!$scope.detailsModal;
        	$scope.getOne(id);
        }
        //查看单个
        $scope.getOne=function(id){
        	$http({
                method: 'POST',
                url: "/admin/sysNoticeType/getSingle",
                data:{
                	id:id
                },
            }).success(function(data) {
                console.info(data);
                $scope.list=data;
            }).error(function(data) {
            });
        }
        //编辑提交
        $scope.saveSubmit = function(){
        	var parm=$scope.list;
            $http({
                method: 'POST',
                url: "/admin/sysNoticeType/edit",
                data:parm,
            }).success(function(data) {
                console.info(data);
                if(data.result=="fail"){
                    toastr.error("保存失败");
                }else{
                    toastr.success("保存成功");
                    $scope.detailsModal = !$scope.detailsModal;
                    $scope.orderParams.reload();

                }

            }).error(function(data) {
            });
        };
    }]);


});